package com.miyu.system.dao.menu;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.plugins.baseDao.BaseDao;
import com.miyu.system.pojo.system.SysMenu;
import org.springframework.stereotype.Repository;

/**
 * 系统菜单
 *
 * @author longm
 * @date 2022/10/04
 */
@Repository
public interface SysMenuDao extends BaseDao<SysMenu,String> {

    /**
     * 查询系统菜单
     *
     * @param param 菜单名称
     * @return {@link JSONArray}
     */
    public default JSONArray queryMenu(JSONObject param){
        JSONArray rs;

        String menuName = "menuName";
        String spaceId = "spaceId";

        StringBuilder sql = new StringBuilder();
        sql.append("select");
        sql.append(" cm.menu_id,");
        sql.append(" cm.file_path,");
        sql.append(" cm.menu_code,");
        sql.append(" cm.is_hidden,");
        sql.append(" cm.is_stop,");
        sql.append(" cm.is_sys,");
        sql.append(" cm.menu_name,");
        sql.append(" cm.menu_icon,");
        sql.append(" cm.router_path ,");
        sql.append(" cm.order_num,");
        sql.append(" cm.parent_id,");
        sql.append(" cm.space_id,");
        sql.append(" cm.menu_type,");
        sql.append(" cm.is_last,");
        sql.append("cm.menu_authority,");
        sql.append("cm.remark,");
        sql.append("cm.is_cache,");
        sql.append("cm.menu_param,");
        sql.append(" cs.space_zone ,");
        sql.append(" cs.space_name ");
        sql.append(" from  sys_menu cm "  );
        sql.append(" left join sys_space cs on cm.space_id = cs.space_id ");
        sql.append(" where 1=1 ");
        if(param.containsKey(menuName)&& StrUtil.isNotBlank(param.getString(menuName))){
            sql.append(" and cm.menu_name like '%").append(param.getString("menuName")).append("%'");

        }
        if(param.containsKey(spaceId)&& StrUtil.isNotBlank(param.getString(spaceId))){
            sql.append(" and cm.space_id = '").append(param.getString("spaceId")).append("'");
        }


        rs = this.queryJSONArrayBySql(sql.toString());

        return rs;
    }

    /**
     * 获取菜单路由
     * @return {@link JSONArray}
     */
    default  JSONArray getRoutes(){
        StringBuffer sql = new StringBuffer();
        sql.append("  select  cm.menu_id,cm.file_path,cm.is_cache,cm.is_hidden,cm.is_last,cm.is_stop,cm.is_sys,cm.menu_authority,cm.menu_icon,cm.menu_name,cm.menu_param,");
        sql.append(" cm.menu_type, cm.order_num,cm.parent_id,cm.remark,cm.router_path,cm.space_id,cm.menu_code, cs.space_name,cs.space_zone ");
        sql.append(" from  sys_menu cm   ");
        sql.append(" left join  sys_space cs on cm.space_id = cs.space_id  ");
        sql.append(" where cm.is_last='1' and cm.is_stop='0'");
        return  this.queryJSONArrayBySql(sql.toString());
    }
}
